﻿using UnityEngine;
using UnityEngine.EventSystems;

public class CellDisplay : MonoBehaviour
{
    public Cell m_Cell;
    public GameObject m_Cover;
    public GameObject m_Highlight;
    SpriteRenderer m_HighlightRenderer;
    SpriteRenderer m_GrassRenderer;

    public Color m_AttackHighlightColor = new Color(1, 0, 0, 0.5f);
    public Color m_MoveHighlightColor = new Color(0, 1, 0, 0.5f);
    private void Start()
    {
        m_Cover = transform.Find("Cover").gameObject;

        m_Highlight = transform.Find("Highlight").gameObject;
        m_HighlightRenderer = m_Cover.GetComponent<SpriteRenderer>();
    }
    public void Init(Cell cell)
    {
        m_Cell = cell;
        if (m_Cell.Type == CellType.Forest)
        {
            m_GrassRenderer = transform.Find("Grass").GetComponent<SpriteRenderer>();
            m_GrassRenderer.sortingOrder = 2 * (cell.Board.Height - cell.Pos.y) + 1;
        }
        m_Cover = transform.Find("Cover").gameObject;

        m_Highlight = transform.Find("Highlight").gameObject;
        m_HighlightRenderer = m_Cover.GetComponent<SpriteRenderer>();
        transform.localPosition = new Vector3(cell.Pos.x, 0f, cell.Pos.y);
    }
    //public void Refresh(Rect rect)
    //{
    //    transform.position = rect.center;
    //    transform.localScale = rect.size;
    //}
    public void SetVisible(Team viewer)
    {
        if (viewer == Team.Blue)
        {
            m_Cover.SetActive(!m_Cell.Visible2Blue);
        }
        else
        {
            m_Cover.SetActive(!m_Cell.Visible2Red);
        }
    }

    public void SetHighlight(bool hightlight, bool isAttackHightlight)
    {
        m_Highlight.SetActive(hightlight);
        if (isAttackHightlight)
        {
            m_HighlightRenderer.color = m_AttackHighlightColor;
        }
        else
        {
            m_HighlightRenderer.color = m_MoveHighlightColor;
        }
    }
}